package metadata

import (
	httpHandler "github.com/edgexfoundry/edgex-go/internal/app/core/metadata/controller/http"
	metadataController "github.com/edgexfoundry/edgex-go/internal/app/core/metadata/controller/http"
	"github.com/edgexfoundry/edgex-go/internal/pkg/correlation"
	"github.com/edgexfoundry/go-mod-bootstrap/v2/bootstrap/container"
	"github.com/edgexfoundry/go-mod-bootstrap/v2/di"
	"github.com/gorilla/mux"
	"net/http"
)

func LoadRestRoutes(r *mux.Router, dic *di.Container) {

	s := r.PathPrefix(ApiBase).Subrouter()

	//Device
	deviceHandler := httpHandler.NewDeviceController(dic)
	s.HandleFunc(ApiDeviceAllRoute, deviceHandler.DevicesGet).Methods(http.MethodGet)
	s.HandleFunc(ApiDeviceRoute, deviceHandler.DeviceAdd).Methods(http.MethodPost)
	s.HandleFunc(ApiDeviceRoute, deviceHandler.DeviceGetById).Methods(http.MethodGet)
	s.HandleFunc(ApiDeviceRoute, deviceHandler.DeviceUpdate).Methods(http.MethodPut)
	s.HandleFunc(ApiDeviceRoute, deviceHandler.DeviceDelete).Methods(http.MethodDelete)
	s.HandleFunc(DeviceStatistic, deviceHandler.CountByDeviceStatus).Methods(http.MethodGet)

	//Device Profile
	deviceProfileHandler := httpHandler.NewDeviceProfileController(dic)
	s.HandleFunc(ApiDeviceProfileAddRoute, deviceProfileHandler.DeviceProfileAdd).Methods(http.MethodPost)
	s.HandleFunc(ApiDeviceProfileRoute, deviceProfileHandler.DeviceProfileById).Methods(http.MethodGet)

	// Device Resource
	deviceResourceHandler := httpHandler.NewDeviceResourceController(dic)
	s.HandleFunc(ApiDeviceResourceRoute, deviceResourceHandler.DeviceResourceAdd).Methods(http.MethodPost)
	s.HandleFunc(ApiDeviceResourceRoute, deviceResourceHandler.DeviceResourceUpdate).Methods(http.MethodPut)
	s.HandleFunc(ApiDeviceResourceRoute, deviceResourceHandler.DeviceResourceDelete).Methods(http.MethodDelete)
	s.HandleFunc(ApiDeviceResourceRoute, deviceResourceHandler.DeviceResourcesGetByDeviceId).Methods(http.MethodGet)

	// Device Command
	deviceCommandHandler := httpHandler.NewDeviceCommandController(dic)
	s.HandleFunc(ApiDeviceCommandRoute, deviceCommandHandler.DeviceCommandAdd).Methods(http.MethodPost)
	s.HandleFunc(ApiDeviceCommandRoute, deviceCommandHandler.DeviceCommandGet).Methods(http.MethodGet)
	s.HandleFunc(ApiDeviceCommandRoute, deviceCommandHandler.DeviceCommandDelete).Methods(http.MethodDelete)
	s.HandleFunc(ApiDeviceCommandRoute, deviceCommandHandler.DeviceCommandUpdate).Methods(http.MethodPut)

	// Device AutoEvent
	s.HandleFunc(ApiAutoEventRoute, deviceHandler.AutoEventAdd).Methods(http.MethodPost)
	s.HandleFunc(ApiAutoEventRoute, deviceHandler.AutoEventGetByDeviceId).Methods(http.MethodGet)
	s.HandleFunc(ApiAutoEventRoute, deviceHandler.AutoEventDelete).Methods(http.MethodDelete)
	s.HandleFunc(ApiAutoEventRoute, deviceHandler.AutoEventUpdate).Methods(http.MethodPut)

	// Device Transport
	s.HandleFunc(ApiTransportRoute, deviceHandler.TransportAdd).Methods(http.MethodPost)
	s.HandleFunc(ApiTransportRoute, deviceHandler.TransportGetByDeviceId).Methods(http.MethodGet)
	s.HandleFunc(ApiTransportRoute, deviceHandler.TransportDelete).Methods(http.MethodDelete)
	s.HandleFunc(ApiTransportRoute, deviceHandler.TransportUpdate).Methods(http.MethodPut)

	s.Use(correlation.ManageHeader)
	s.Use(correlation.LoggingMiddleware(container.LoggingClientFrom(dic.Get)))

	// 产品分组 ProfileCategory
	catgory := metadataController.NewProductCategoryServiceController(dic)
	r.HandleFunc(ApiAllProdcutCategoryServiceRoute, catgory.GetProductCategorysList).Methods(http.MethodGet) // 产品分组列表查询
	r.HandleFunc(ApiProductCategoryByNameRoute, catgory.ProdcutCategoryByName).Methods(http.MethodGet)       // 产品分组模糊查询
	r.HandleFunc(ApiOwnProdcutCategoryServiceRoute, catgory.ProdcutCategoryOwn).Methods(http.MethodGet)      // 产品分组查询-不分页

	r.HandleFunc(ApiProductCategoryServiceRoute, catgory.AddProductCategory).Methods(http.MethodPost)      // 产品分组新增
	r.HandleFunc(ApiProductCategoryUpdateRoute, catgory.UpdateProductCategory).Methods(http.MethodPost)    // 产品分组修改
	r.HandleFunc(ApiProductCategoryDeleteRoute, catgory.DeleteProductCategoryById).Methods(http.MethodGet) // 产品分组删除

	// Product Controller
	pc := httpHandler.NewProductController(dic)
	// 添加产品
	s.HandleFunc("/product", pc.AddProduct).Methods(http.MethodPost)
	// 分页查询全部产品
	s.HandleFunc("/product/all", pc.AllProducts).Methods(http.MethodGet)
	// 查询全部产品-查询公开的产品
	s.HandleFunc("/product/own", pc.OwnProducts).Methods(http.MethodGet)
	// 根据产品名称查询产品
	//s.HandleFunc("/product/name/{name}", pc.ProductByName).Methods(http.MethodGet)
	// 根据产品id查询产品
	s.HandleFunc("/product/id/{id}", pc.ProductById).Methods(http.MethodGet)
	// 删除产品
	s.HandleFunc("/product/{productid}", pc.ProductDelete).Methods(http.MethodDelete)
	// 更新产品
	s.HandleFunc("/product", pc.ProductUpdate).Methods(http.MethodPut)
	// 根据名称模糊查询
	//s.HandleFunc("/products/name/{name}", pc.ProductsByName).Methods(http.MethodGet)
	// 分页查询全部产品,带该产品的设备数量
	//s.HandleFunc("/products/with/devicenum", pc.AllProductsWithDeviceNum).Methods(http.MethodGet)

	// 查询产品的profile
	s.HandleFunc("/product/profile/{productid}", pc.ProductProfileByProductId).Methods(http.MethodGet)
	// 根据产品分类分页查询产品
	//s.HandleFunc("/product/group/{groupid}", pc.ProductsByGroupId).Methods(http.MethodGet)

	// Attribute of Product Controller
	pac := httpHandler.NewProductAttributeController(dic)
	// 添加产品属性
	s.HandleFunc("/product/attribute", pac.AddProductAttribute).Methods(http.MethodPost)
	// 分页查询某个产品的属性
	s.HandleFunc("/product/attribute/productid/{productid}", pac.AttributesByProductId).Methods(http.MethodGet)
	// 批量删除产品属性
	s.HandleFunc("/product/attribute/delete", pac.ProductAttributeDelete).Methods(http.MethodPost)
	// 修改产品属性
	s.HandleFunc("/product/attribute", pac.UpdateProductAttribute).Methods(http.MethodPut)

	// Method of Product Controller
	pmc := httpHandler.NewProductFunctionController(dic)
	// 添加产品功能
	s.HandleFunc("/product/function", pmc.AddProductFunction).Methods(http.MethodPost)
	// 根据产品id查询产品的功能
	s.HandleFunc("/product/function/productid/{productid}", pmc.AllFunctionsByProductId).Methods(http.MethodGet)
	// 批量删除产品功能
	s.HandleFunc("/product/function/delete", pmc.ProductFunctionDelete).Methods(http.MethodPost)
	// 更新产品功能
	s.HandleFunc("/product/function", pmc.UpdateProductFunction).Methods(http.MethodPut)

	// 产品传输配置
	ptc := httpHandler.NewProductTransportController(dic)
	// 添加产品传输配置
	s.HandleFunc("/product/transport", ptc.AddProductTransport).Methods(http.MethodPost)
	// 根据产品id分页查询产品传输配置
	s.HandleFunc("/product/transport/productid/{productid}", ptc.AllTransportsByProductId).Methods(http.MethodGet)
	// 批量删除产品传输配置
	s.HandleFunc("/product/transport/delete", ptc.ProductTransportDelete).Methods(http.MethodPost)
	// 更新产品传输配置
	s.HandleFunc("/product/transport", ptc.UpdateProductTransport).Methods(http.MethodPut)
}
